package com.whf.Controller;

import com.whf.dataobject.ProductCategory;
import com.whf.enums.ResultEnum;
import com.whf.exception.SellException;
import com.whf.form.CategoryForm;
import com.whf.service.CategoryService;
import com.whf.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/18 0018.
 */
@Controller
@RequestMapping("/seller/category")
@Slf4j
public class SellerCategoryController {
    @Autowired
    private CategoryService categoryService;

    //获取所有商品类目列表
    @GetMapping("/list")
//    该注解会在第一次访问改方法时执行方法,以后会直接从redis中查询

    public ModelAndView list(Map<String,Object> map){
        List<ProductCategory> categoryList = categoryService.findAll();
        map.put("categoryList",categoryList);
        return new ModelAndView("/category/list",map);
    }

    //修改以有类目
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                               Map<String,Object> map){
        //根据类目id查询类目信息
        try {
            if(categoryId!=null){
                ProductCategory productCategory = categoryService.findOneById(categoryId);
                map.put("productCategory",productCategory);
            }
            return new ModelAndView("/category/index", map);
        } catch (SellException e) {
            log.error("[卖家端修改类目] 修改类目出现错误");
            map.put("msg",ResultEnum.FAIL_UPDATE_CATEGORY.getMessage());
            map.put("url","/sell/seller/category/list");
            return new ModelAndView("common/error",map);
        }


    }

    @PostMapping("/save")
    public ModelAndView save(@Valid CategoryForm categoryForm,
                             BindingResult bindingResult,
                             Map<String,Object> map){
        if(bindingResult.hasErrors()){
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/category/index?categoryId=" + categoryForm.getCategoryId());
            return new ModelAndView("common/error", map);
        }
        ProductCategory productCategory=new ProductCategory();
        //判断是新增还是修改
        if(categoryForm.getCategoryId()!=null){
            //修改
            productCategory = categoryService.findOneById(categoryForm.getCategoryId());
        }
        BeanUtils.copyProperties(categoryForm,productCategory);
        try {
            ProductCategory save = categoryService.save(productCategory);
            map.put("msg",ResultEnum.SUCCESS_SAVE_CATEGORY.getMessage());
            map.put("url","/sell/seller/category/list");
            return new ModelAndView("common/success",map);
        } catch (SellException e) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/category/index?categoryId=" + categoryForm.getCategoryId());
            return new ModelAndView("common/error", map);
        }
    }

}
